//
//  ChangeEmailViewController.m
//  RENTVISITOR
//
//  Created by wonder on 2017/8/30.
//  Copyright © 2017年 UNN. All rights reserved.
//

#import "ChangeEmailViewController.h"
#import "RentVieitor.h"

@interface ChangeEmailViewController ()<UITextFieldDelegate>

@end

@implementation ChangeEmailViewController {
    
    UITextField *emailText;
}

- (void)viewDidLoad {
    [super viewDidLoad];
   
    self.title = @"设置邮箱";
    self.view.backgroundColor = BackgroundColor;
    [self creatLeftView];
    [self creatView];
}

- (void)creatLeftView {
    
    // 自定义导航栏左侧按钮
    UIButton * leftBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    leftBtn.frame = CGRectMake(0, 0, 80,15 );
    [leftBtn setImage:[UIImage imageNamed:@"箭头左"] forState:UIControlStateNormal];
    [leftBtn setTitle:@"返回" forState:UIControlStateNormal];
    leftBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    [leftBtn setTitleEdgeInsets:UIEdgeInsetsMake(0, 10, 0, 0)];
    leftBtn.titleLabel.font = [UIFont systemFontOfSize:14];
    [leftBtn setTitleColor:[Utils colorConvertFromString:@"#333333"] forState:UIControlStateNormal];
    [leftBtn addTarget:self action:@selector(backAction) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem * leftItem = [[UIBarButtonItem alloc] initWithCustomView:leftBtn];
    self.navigationItem.leftBarButtonItem = leftItem;
    
    // 自定义导航栏左侧按钮
    UIButton * rightBtn = [UIButton buttonWithType:UIButtonTypeSystem];
    rightBtn.frame = CGRectMake(0, 0, 50, 20);
    [rightBtn setTitle:@"下一步" forState:UIControlStateNormal];
    [rightBtn setTitleColor:[UIColor colorWithRed:225/255.0 green:141/255.0 blue:43/255.0 alpha:1] forState:UIControlStateNormal];
    [rightBtn addTarget:self action:@selector(nextAction) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem * rightItem = [[UIBarButtonItem alloc] initWithCustomView:rightBtn];
    self.navigationItem.rightBarButtonItem = rightItem;
    
    UIView *bdView = [[UIView alloc] initWithFrame:CGRectMake(0, 60, KWidth, 50)];
    bdView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:bdView];
    
    emailText = [[UITextField alloc] initWithFrame:CGRectMake(20, 10, KWidth-30, 30)];
    emailText.placeholder = @"请输入新邮箱地址";
    emailText.delegate = self;
    emailText.textColor = [Utils colorConvertFromString:@"#333333"];
    emailText.font = [UIFont systemFontOfSize:15];
    
    UIButton *canceBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    canceBtn.frame = CGRectMake(0, 0, 15, 15);
    [canceBtn setBackgroundImage:[UIImage imageNamed:@"关闭"] forState:UIControlStateNormal];
    [canceBtn addTarget:self action:@selector(canceAction) forControlEvents:UIControlEventTouchUpInside];
    emailText.rightView = canceBtn;
    emailText.rightViewMode = UITextFieldViewModeAlways;
    [bdView addSubview:emailText];
}

- (void)creatView {
    
    UILabel *proLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 10, KWidth, 50)];
    proLabel.text = [NSString stringWithFormat:@"当前邮箱: %@",self.email];
    proLabel.textColor = [Utils colorConvertFromString:@"#666666"];
    proLabel.font = [UIFont systemFontOfSize:16];
    proLabel.textAlignment = NSTextAlignmentCenter;
    [self.view addSubview:proLabel];
    
    

}

- (void)backAction {
    
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)nextAction {
    
    if (emailText.text.length == 0) {
        
        [APPDELEGATE showPro:@"请输入邮箱地址"];
    }
    
    
    
    NSDictionary *dataDic = User_listsValue;
    NSString *number = dataDic[@"number"];
    NSString *token = dataDic[@"token"];
    NSString *urlstr= [NSString stringWithFormat:@"%@member/safty/email",Main_Url];
    NSString *md5String = [NSString stringWithFormat:@"%@%@%@%@%@%@",self.auth,emailText.text,number,token,APPKEY_URL,urlstr];
    //32位小写
    md5String = [LCMD5Tool MD5ForLower32Bate:md5String];
    md5String = [md5String stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
    NSDictionary *param = @{
                            @"number":number,
                            @"token":token,
                            @"auth":self.auth,
                            @"email":emailText.text,
                            @"sign":md5String
                            };
    AFHTTPSessionManager *manger=[AFHTTPSessionManager manager];
    manger.responseSerializer=[AFHTTPResponseSerializer serializer];
    [manger POST:urlstr parameters:param progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        
        NSDictionary* jsonDic = [NSJSONSerialization
                                 JSONObjectWithData:responseObject
                                 options:kNilOptions
                                 error:nil];
        
        if ([jsonDic[@"code"] isEqualToString:@"10000"]) {
            
            [APPDELEGATE showEditSuccess:@"邮箱设置成功"];
            [[NSNotificationCenter defaultCenter] postNotificationName:@"update" object:nil userInfo:nil];
            
            
        }else {
            
            [APPDELEGATE showPro:jsonDic[@"message"]];
            
        }
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        [APPDELEGATE showPro:@"网络错误"];
    }];

}

- (void)canceAction {
    
    emailText.text = @"";
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [self.view endEditing:YES];
}


@end
